package edu.xlh.travel.redis.listener;

import edu.xlh.travel.common.OnlineUserInfoInRedisConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 复述,关键失效侦听器
 *
 * @author wentao.wu
 * @date 2021/12/03
 */
@Component
@Slf4j
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    private final RedisTemplate redisTemplate;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer, RedisTemplate redisTemplate) {
        super(listenerContainer);
        this.redisTemplate = redisTemplate;
    }

    /**
     * orvertime对关键
     */
    private final String ORVERTIME_OVER_KEY = OnlineUserInfoInRedisConstant.OnlineUserInfo_RD_RESOURCES+"::";

    /**
     * 针对redis数据失效事件，进行数据处理
     *
     * @param message Redis Body Message
     * @param pattern Bytes Line
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expireKey = new String(message.getBody());
        if (expireKey.startsWith(ORVERTIME_OVER_KEY)) {
            log.info("失效的消息 body = {} ", expireKey);
        } else {
            log.debug("无需处理失效消息KEY:{}", expireKey);
        }
    }

}
